package com.yulookbook.web;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.lang.StringUtils;

import com.yulookbook.util.Cache;
import com.yulookbook.web.util.CacheFactory;

/**
 * Servlet Filter implementation class HttpContentFilter
 */
public class HttpContentFilter implements Filter {

    /**
     * Default constructor. 
     */
    public HttpContentFilter() {
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		response.setCharacterEncoding("utf-8");
		BaseRequestParam params = new BaseRequestParam(request);
		String format = params.getParam("format");
		if(StringUtils.isNotBlank(format)){
			if(StringUtils.endsWithIgnoreCase("gson", format)){
		    	response.setContentType("application/json; charset=UTF-8");
			}else if(StringUtils.endsWithIgnoreCase("html", format)){
				response.setContentType("text/html; charset=UTF-8");
			}
		}

		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
